package co.chuangdian.oflauncher.uc;

import java.io.File;
import org.json.JSONArray;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import cn.onekit.CONFIG;
import cn.onekit.FSO;
import cn.onekit.JSON;
import cn.onekit.android.ACTIVITY;
import co.chuangdian.oflauncher.R;

public class FolderSideBar extends LinearLayout {
	ListView destFolderListView;

	public FolderSideBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setOrientation(LinearLayout.VERTICAL);
		View.inflate(getContext(), R.layout.uc_foldersidebar, this);
		destFolderListView = (ListView) this.findViewById(R.id.destFolderListView);
		setData();
	}

	public boolean mode;

	public int pointToPosition(int x, int y) {
		return destFolderListView.pointToPosition(x-ACTIVITY.dp2px(50), y);
	}

	void setData() {
		final JSONArray folders = JSON.parses((String) CONFIG.get("folders"));
		destFolderListView.setAdapter(new BaseAdapter() {

			@Override
			public int getCount() {
				return folders.length();
			}

			@Override
			public Object getItem(int position) {
				return null;
			}

			@Override
			public long getItemId(int position) {
				return 0;
			}

			@SuppressLint("ViewHolder")
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ViewGroup row = (ViewGroup) View.inflate(getContext(), R.layout.folders_row_mini, null);
				row.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(100)));
				ImageView coverImageView = (ImageView) row.findViewById(R.id.coverImageView);
				//
				JSONObject folder = folders.optJSONObject(position);
				File path = FSO.photos(folder.optString("cover"));
				Bitmap cover = FSO.loadImage(path.getPath(), true);
				coverImageView.setImageBitmap(cover);
				return row;
			}

		});
	}
}
